<?php

//菜单管理类

namespace Home\Controller;

use Think\Controller;

class MenuController extends BaseController {

    private $menukeyArr = array("id", "pid", "name", "type", "key", "url", "sort");
    private $menuValArr = array(0, 0, '', 0, 0, 0, 0);
    private $gpNameArr = array("group3", "group2", "group1");

    public function handle_menu_data() {
        $data = $_GET;
        if ($_GET["id"]) {
            $this->edit_menu($data);
            $result =trim($_GET["id"]);
            
        } else {
            unset($data["id"]);
            $result =$this->add_menu($data);
        }
        echo $result;
    }

    public function index() {
        $appid = $_SESSION["app"]["id"];
        $mmod = D("Menu");

        $where = array("app_id" => array("eq", $appid), "del" => array("eq", 0));
        $field = "id,pid,name,type,key,url,sort";

        $menusData = $mmod->where($where)->order("sort desc")->getField($field);
        //= $mmod->select_menu($where, $field, "sort desc");
        //print_r($menusData);exit;
        if (!$menusData) {
            $groups = $this->create_empty_menus();
        } else {
            $groups = $this->fix_empty_menu($menusData);
        }
        $this->assign("group1", $groups["group1"]);
        $this->assign("group2", $groups["group2"]);
        $this->assign("group3", $groups["group3"]);
        $this->display("add_menu");
    }

    private function add_menu($data) {
        $mmod = D("Menu");
        $data["app_id"] = $_SESSION["app"]["id"];
        $lastId = $mmod->data($data)->add();
        if ($lastId) {
            $this->update_wx_menu();
            return $lastId;
        }
    }

    private function edit_menu($data) {
        $where = array("id" => array("eq", trim($data["id"])));
        unset($data["id"]);

        $mmod = D("Menu");

        if (empty($data["name"])) {
            $field = "pid,id";
            $result = $mmod->find_menu($where, $field);
            $targetData = array("del" => 1);
            if ($result["pid"] != 0) {
                $mmod->update_menu($where, $targetData);
                $result = $this->update_wx_menu();
            } else {

                $where = array("pid" => array("eq", $result["pid"]));
                $field = "count(*)";
                $count = $mmod->select($where, $field);
                if ($count == 0) {
                    $mmod->update_menu($where, $targetData);
                    $result = $this->update_wx_menu();
                }
            }
        } else {
            $mmod->update_menu($where, $data);
            $result = $this->update_wx_menu();
        }

        echo $result;
    }

    public function update_wx_menu() {
        $mmod = D("Menu");
        $where = array("del" => array("eq", 0));
        $field = "id,pid,name,type,key,url";
        $MenuData = $mmod->select_menu($where, $field, "sort desc");

        $j = 0;
        foreach ($MenuData as $k => $v) {
            if ($v["pid"] == 0) {

                $data["button"][$j]["id"] = $v["id"];
                $data["button"][$j]["name"] = $v["name"];
                $data["button"][$j]["type"] = $v["type"] == 1 ? "click" : "view";
                $data["button"][$j]["key"] = $v["key"] ? $v["key"] : "";
                $data["button"][$j]["url"] = $v["url"] ? $v["url"] : "";
                $data["button"][$j] = array_filter($data["button"][$j]);
                $j++;
            }
        }


        foreach ($data["button"] as &$dv) {
            $i = 0;
            foreach ($MenuData as $mk => $mv) {
                if ($dv["id"] == $mv["pid"]) {
                    $dv["sub_button"][$i]["name"] = $mv["name"];
                    $dv["sub_button"][$i]["type"] = $mv["type"] == 1 ? "click" : "view";
                    $dv["sub_button"][$i]["key"] = $mv["key"] ? $mv["key"] : "";
                    $dv["sub_button"][$i]["url"] = $mv["url"] ? $mv["url"] : "";
                    $dv["sub_button"][$i] = array_filter($dv["sub_button"][$i]);
                    $i++;
                    unset($dv["type"]);
                }
            }
            unset($dv["id"]);
        }

        $result = $this->edit_wx_menu($data);

        print_r($result);
    }

    public function del_menu(){
        $id = $_GET["id"];
        $mmod = D("Menu");
        
        $mmod->where("id=".$id)->save(array("del"=>1));
        
    }

    public function fill_data() {
        $mmod = D("menu");
        $menuData = $this->get_wx_menu();

        foreach ($menuData["menu"]["button"] as $v) {
            $data["pid"] = 0;
            $data["app_id"] = $_SESSION["app"]["member_id"];
            $data["name"] = $v["name"];
            $data["type"] = $v["type"] == "click" ? 1 : 0;
            $data["key"] = $v["key"] ? $v["key"] : "";
            $data["url"] = $v["url"] ? $v["url"] : "";
            $lastId = $mmod->add_menu($data);
            unset($data);
            if (!empty($v["sub_button"])) {
                foreach ($v["sub_button"] as $vv) {
                    $data["pid"] = $lastId;
                    $data["app_id"] = $_SESSION["app"]["member_id"];
                    $data["name"] = $vv["name"];
                    $data["type"] = $vv["type"] == "click" ? 1 : 0;
                    $data["key"] = $vv["key"] ? $vv["key"] : "";
                    $data["url"] = $vv["url"] ? $vv["url"] : "";
                    $mmod->add_menu($data);
                    unset($data);
                }
            }
        }
    }

    private function create_empty_menus() {

        $x = 3;
        $y = 6;
        $temp = array();

        for ($i = 1; $i <= $x; $i++) {
            $temp["group" . $i] = array();
            for ($j = 1; $j <= $y; $j++) {
                $temp["group" . $i][] = array_combine($this->menukeyArr, $this->menuValArr);
            }
        }
        return $temp;
    }

    private function fix_empty_menu($data) {

        $rootArr = array();
        $branchArr = array();
        $temp = array();
        foreach ($data as $k => $v) {
            if ($v["pid"] == 0) {
                $temp["group" . $v["sort"]][6] = $v;
            } else {
                $temp["group" . $data[$v["pid"]]["sort"]][$v["sort"]] = $v;
            }
        }

        $gpNameKey = array_keys($temp);

        $diff = array_diff($this->gpNameArr, $gpNameKey);
        if ($diff) {
            foreach ($diff as $dk => $dv) {
                $temp[$dv] = array();
                for ($j = 1; $j <= 6; $j++) {
                    $temp[$dv][] = array_combine($this->menukeyArr, $this->menuValArr);
                }
            }
        }

        foreach ($temp as $tk => &$tv) {
            if (count($tv) < 6) {
                $keysArr = array_keys($tv);
                for ($i = 1; $i <= 6; $i++) {
                    if (!in_array($i, $keysArr)) {
                        $tv[$i] = array_combine($this->menukeyArr, $this->menuValArr);
                    }
                }
            }

            ksort($tv);
        }


        foreach ($this->gpNameArr as $k) {
            $temp[$k] = $temp[$k];
        }


        return $temp;
    }

}